package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import model.Teacher;

/**
 *
 * @author cska
 *
 */
public class TeacherDlg extends JDialog implements ocDialog {

    protected GridBagConstraints c;
    protected JTextField fio;
    protected JTextField rank;
    protected JTextField login;
    protected JTextField pass;
    protected JPanel buttonPanel;
    protected JButton genPass, ok, cancel;
    protected JLabel l1, l2, l3, l4;

    public boolean fl;
    protected Teacher teacher;

    public TeacherDlg(JFrame owner, boolean modal, String title, Teacher teacher) {
        super(owner, modal);
        setTitle(title);
        setLayout(new GridBagLayout());

        fl = false;
        if (teacher != null) this.teacher = teacher; else this.teacher = new Teacher("", "", "", "");

        c = new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

        c.weightx = 0.1;
        l1 = new JLabel("Ф.И.О.:", JLabel.RIGHT);
        add(l1,c);
        c.gridy = 1;
        l2 = new JLabel("Должность:", JLabel.RIGHT);
        add(l2,c);
        c.gridy = 2;
        l3 = new JLabel("Пользователь:", JLabel.RIGHT);
        add(l3,c);
        c.gridy = 3;
        c.gridheight = 2;
        l4 = new JLabel("Пароль:", JLabel.RIGHT);
        add(l4,c);

        c.weightx = 0.9;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 0;
        fio = new JTextField(this.teacher.getFio(), 5);
        fio.setHorizontalAlignment(JTextField.CENTER);
        add(fio,c);
        c.gridy = 1;
        rank = new JTextField(this.teacher.getRank(), 5);
        rank.setHorizontalAlignment(JTextField.CENTER);
        add(rank,c);
        c.gridy = 2;
        login = new JTextField(this.teacher.getLogin(), 5);
        login.setHorizontalAlignment(JTextField.CENTER);
        add(login,c);
        c.gridy = 3;
        pass = new JTextField(this.teacher.getPass(), 5);
        pass.setHorizontalAlignment(JTextField.CENTER);
        add(pass,c);
        c.gridy = 4;
        genPass = new JButton("Сгенерировать");
        genPass.addActionListener(new genPassAdapter(pass));
        add(genPass,c);

        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.CENTER;
        c.anchor = GridBagConstraints.EAST;
        c.weightx = 1.0;
        c.insets = new Insets(0, 0, 0, 5);
        buttonPanel = new JPanel(new GridBagLayout());
        ok = new JButton("Сохранить");
        ok.addActionListener(new okAdapter(this));
        buttonPanel.add(ok,c);
        c.gridx = 1;
        c.weightx = 0.0;
        c.insets = new Insets(0, 5, 0, 0);
        cancel = new JButton("Отмена");
        cancel.addActionListener(new cancelAdapter(this));
        buttonPanel.add(cancel,c);
        c.gridx = 0;
        c.gridy = 5;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth = 2;
        c.weightx = 1.0;
        c.insets = new Insets(5, 5, 5, 5);
        add(buttonPanel,c);

        setSize(400,240);
        Dimension screen = getToolkit().getScreenSize().getSize();
        setLocation((screen.width-400)/2, (screen.height-240)/2);
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        setVisible(true);

    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void ok() {
        fl = true;
        teacher.setFio(fio.getText());
        teacher.setRank(rank.getText());
        teacher.setLogin(login.getText());
        teacher.setPass(pass.getText());
        setVisible(false);
    }

    public void cancel() {
        fl = false;
        setVisible(false);
    }

    class genPassAdapter implements ActionListener {

        protected JTextField pass;

        public genPassAdapter(JTextField pass) {
            this.pass = pass;
        }

        public void actionPerformed(ActionEvent e) {
            String s = "";
            Random rand = new Random();
            for (int i = 0; i < 8; ++i) {
                int d = 0;
                do {
                    d = rand.nextInt(256);
                } while (!(((d >= '0') && (d <= '9')) || ((d >= 'A') && (d <= 'Z')) || ((d >= 'a') && (d <= 'z'))));
                s += (char) d;
            }
            pass.setText(s);
        }

    }

}
